package com.tac.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tac.common.Exception.CosTomException;
import com.tac.common.result.Result;
import com.tac.dao.HotelDao;
import com.tac.pojo.Collection;
import com.tac.pojo.Hotel;
import com.tac.pojo.vo.HotelVo;
import com.tac.service.CollectionService;
import com.tac.service.HotelService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Base64Utils;
import org.springframework.util.DigestUtils;

import javax.xml.crypto.Data;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

@Service
public class HotelServiceImpl extends ServiceImpl<HotelDao, Hotel> implements HotelService {

    @Autowired
    private CollectionService collectionService;
    /**
     * 查看所有酒店
     * @return
     */
    public Result< List<Hotel>> seleceAll(){
        List<Hotel> list = this.list();
        return Result.success(list,"success");
    }

    /**
     * 查看酒店详情
     * @param hid
     * @param uid
     * @return
     */
    public Result<HotelVo> selectById(Long hid, Long uid){
        HotelVo hotelVo = new HotelVo();
        Hotel hotel = this.getById(hid);
        BeanUtils.copyProperties(hotel,hotelVo);
            if(uid!=null) {
                Collection collection = new Collection();
                collection.setUserId(uid);
                collection.setTargetId(hid);
                Collection one = collectionService.getOne(Wrappers.query(collection));
                if (one == null) {
                    hotelVo.setCollection(false);
                } else {
                    hotelVo.setCollection(true);
                }
            }
        return Result.success(hotelVo,"success");
    }

    /**
     * 收藏酒店
     * @param hid
     * @param uid
     * @return
     */
    public Result collection(Long hid, Long uid){
        //判断是否已经收藏

        Collection collection = new Collection();
        collection.setTargetId(hid);
        collection.setUserId(uid);
        collection.setType("hotel");
        Collection one = collectionService.getOne(Wrappers.query(collection));
        if(one!=null){
            throw new CosTomException("已经收藏过了");
        }
        boolean save = collectionService.save(collection);
        if(!save){
            throw new CosTomException("收藏失败");
        }
        return Result.success(null,"success");
    }

    /**
     * 取消收藏
     * @param hid
     * @param uid
     * @return
     */
    public Result deleteByCollection(Long hid, Long uid){
        Collection collection = new Collection();
        collection.setTargetId(hid);
        collection.setUserId(uid);
        collection.setType("hotel");
        boolean remove = collectionService.remove(Wrappers.query(collection));
        if(!remove){
            throw new CosTomException("取消收藏失败");
        }
        return Result.success(null,"success");

    }

    /**
     * 查看所有收藏的酒店
     * @param uid
     * @return
     */
    public Result<List<Hotel>> selectBycollection(Long uid){
        Collection collection = new Collection();
        collection.setUserId(uid);
        collection.setType("hotel");
        List<Collection> list = collectionService.list(Wrappers.query(collection));
        ArrayList<Hotel> arrayList = new ArrayList<>();
        for(Collection collection1:list){
            Long targetId = collection1.getTargetId();
            Hotel hotel = this.getById(targetId);
            arrayList.add(hotel);
        }
        return Result.success(arrayList,"success");
    }

    /**
     * 按创建时间搜索酒店
     * @param page
     * @param pagesize
     * @return
     */
    public Result<Page<Hotel>> selectByTime(Long page, Long pagesize, String startTime,String endTime){
        Page<Hotel> hotelPage = new Page<>(page, pagesize);
        LambdaQueryWrapper<Hotel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.between(Hotel::getCreateTime,startTime,endTime);
        this.page(hotelPage,queryWrapper);
        return Result.success(hotelPage,"success");
    }

}
